import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Users from '../components/user/Users'
import Rights from '../components/power/Rights'
import Roles from '../components/power/Router'
import Cate from '../components/goods/Cate'
import Params from '../components/goods/Params'
import List from '../components/goods/List'
import AddGoods from '../components/goods/Add'
import Order from '../components/order/Order'
import Report from '../components/report/Report'
/* 将路由注册到全局 */
Vue.use(VueRouter)

/* 定义路由规则 */
const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    /* 默认重定向到该路径 */
    redirect: '/home/welcome',
    children: [
      { path: 'welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: AddGoods },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }

]

const router = new VueRouter({
  routes
})
/* 设置路由导航控制访问权限 */
router.beforeEach((to, from, next) => {
  /* 判断是否访问的登录页 */
  if (to.path === '/login') return next()
  /* 如果访问的不是登录页 判断是否存在token */
  const token = window.sessionStorage.getItem('token')
  /* 如果没有token就直接跳转的登录页 */
  // console.log(token)
  if (!token) return next('/login')
  /* 如果有就直接放行 */
  next()
})
export default router
